feat: add Knex.js ORM (SQLite/Postgres/MariaDB)#15
Open
volkermauel wants to merge 1 commit intocoolstartnow:mainfrom
Open
feat: add Knex.js ORM (SQLite/Postgres/MariaDB)#15volkermauel wants to merge 1 commit intocoolstartnow:mainfrom
volkermauel wants to merge 1 commit intocoolstartnow:mainfrom
Conversation
…iaDB) - Add centralized knexDatabase.js with singleton init, auto-schema, and multi-backend support - Create Knex-based store modules under server/db/stores/ for all domains - Update all routes to use async Knex stores with STORAGE_BACKEND env var - Make acknowledgements route async (getMode, effectiveMode) - Promote TEXT columns to MEDIUMTEXT for MariaDB compatibility - Fix test beforeAll to await Knex init for non-JSON backends - Add tools/migrate-json-to-knex.js for migrating existing JSON data - Add knex and pg dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace JSON File Storage with Knex.js ORM
Summary
Replaces the flat JSON file storage layer with Knex.js, enabling the ISMS Builder to run against SQLite, PostgreSQL, and MariaDB/MySQL backends while keeping the original JSON mode as a zero-config default.
Motivation
What Changed
New: Centralized Database Module (
server/db/knexDatabase.js)init()/destroy()initSchema)ensureColumns) — safe for incremental deploysSTORAGE_BACKENDenv var selects the driver (json→ original,sqlite→ better-sqlite3,postgres→ pg,mariadb→ mysql2)New: Knex-based Store Modules (
server/db/stores/*.js)21 new store modules that mirror the existing JSON store API but execute against Knex:
templateStore·ackStore·assetStore·auditStore·bcmStore·customListsStore·entityStore·findingStore·gdprStore·goalsStore·governanceStore·guidanceStore·legalStore·orgSettingsStore·orgUnitStore·publicIncidentStore·riskStore·soaStore·supplierStore·trainingStoreModified: Routes & Storage Layer
All 22 route modules updated to
awaitthe async store calls. Theserver/storage.jsfacade now delegates to Knex stores whenSTORAGE_BACKEND !== 'json'.Modified: Existing Store Shims (
server/db/*.js)The original JSON stores now re-export from
server/db/stores/to preserve backward compatibility for any direct imports.New: Migration Tool (
tools/migrate-json-to-knex.js)Idempotent CLI script that reads all
data/*.jsonfiles and inserts them into the configured Knex backend. Safe to re-run — skips existing rows by primary key.Database-specific Fixes
TEXTcolumns automatically promoted toMEDIUMTEXTto support large document bodies (e.g. guidance content with embedded OpenAPI specs).INTEGER GENERATED ALWAYS AS IDENTITYfor auto-increment primary keys.getMode()and distribution creation made fully async.Configuration
STORAGE_BACKENDjson,sqlite,postgres,mariadbjsonDATA_DIR./dataDB_HOSTDB_PORTDB_USERDB_PASSDB_NAMESTORAGE_BACKEND=jsonor unset) is unchanged — the app reads/writes JSON files exactly as before.Testing
Files Changed
68 files: +6 527 / −743 lines
server/db/knexDatabase.js,server/db/stores/*.js,tools/migrate-json-to-knex.jsstorage.jsBreaking Changes
None. The JSON storage backend remains the default and is fully backward-compatible.
Deployment Notes
STORAGE_BACKEND=sqlite(orpostgres/mariadb) and run the migration script.